﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region FactoryCertificateDao

        public List<tb_RPT_FactoryCertificate> GetAllFactoryCertificate()
        {
            var query = (from obj in this.tb_RPT_FactoryCertificates
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_FactoryCertificate> GetFactoryCertificateBySequence(string strSequence)
        {

            var query = (from obj in this.tb_RPT_FactoryCertificates
                         where obj.IsDelete == 0 && obj.Sequence == strSequence
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_FactoryCertificate> GetFactoryCertificateByProjectName(string strProjectName)
        {
            var query = (from obj in this.tb_RPT_FactoryCertificates
                         where obj.IsDelete == 0 && obj.ProjectName == strProjectName
                         select obj).ToList();
            return query;
        }

        public List<tb_RPT_FactoryCertificate> GetFactoryCertificateByContition(string strProjectName, string strSequence, string strBuyer, string strSupplier)
        {
            var tq = from obj in this.tb_RPT_FactoryCertificates
                     where obj.IsDelete == 0
                     select obj;

            if (strProjectName.Length > 0)
            {
                tq = tq.Where(entity => entity.ProjectName == strProjectName);
            }
            if (strSequence.Length > 0)
            {
                tq = tq.Where(entity => entity.Sequence == strSequence);
            }

            if (strBuyer.Length > 0)
            {
                tq = tq.Where(entity => entity.Buyer == strBuyer);
            }
            if (strSupplier.Length > 0)
            {
                tq = tq.Where(entity => entity.Supplier == strSupplier);
            } 

            var query = tq.ToList();
            return query;
        }

        public tb_RPT_FactoryCertificate GetFactoryCertificateById(int id)
        {
            return this.tb_RPT_FactoryCertificates.SingleOrDefault(tb_RPT_FactoryCertificates => tb_RPT_FactoryCertificates.Id == id);
        }

        public void CreateFactoryCertificate(tb_RPT_FactoryCertificate entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_RPT_FactoryCertificates.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyFactoryCertificate(tb_RPT_FactoryCertificate entity)
        {
            tb_RPT_FactoryCertificate instance = this.tb_RPT_FactoryCertificates.SingleOrDefault(p => p.Id == entity.Id);
            instance.Sequence = entity.Sequence;
            instance.ProjectName = entity.ProjectName;
            instance.CastingParts = entity.CastingParts;
            instance.Buyer = entity.Buyer;
            instance.ConcreteGrade = entity.ConcreteGrade;
            instance.MixNumbers = entity.MixNumbers;
            instance.CementVarieties = entity.CementVarieties;
            instance.DeliveryDate = entity.DeliveryDate;
            instance.Supplier = entity.Supplier;
            instance.FactorySlump = entity.FactorySlump;
            instance.Volume = entity.Volume;
            instance.CementManufacturers = entity.CementManufacturers;
            instance.Result = entity.Result;
            instance.Review = entity.Review;
            instance.Approval = entity.Approval;
            instance.Test = entity.Test; 
         

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;

            this.SubmitChanges();
        }

        public void DeleteFactoryCertificate(tb_RPT_FactoryCertificate entity)
        {
            tb_RPT_FactoryCertificate deleteEntity = tb_RPT_FactoryCertificates.SingleOrDefault(p => p.Id == entity.Id);
            tb_RPT_FactoryCertificates.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

        #endregion

    }

}
